package com.quanjiaolian.userinfo.serviceimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.quanjiaolian.DB.DataBase;
import com.quanjiaolian.bean.InsInfo;
import com.quanjiaolian.bean.UserInfo;
import com.quanjiaolian.tools.Utils;
import com.quanjiaolian.userinfo.service.UserInfoService;

public class UserInfoServiceImpl implements UserInfoService {
	

	/**
	 * 声明Log4j变量
	 */
	public static ResourceBundle resourceBundle = null;
	static Logger logger = Logger.getLogger(UserInfoServiceImpl.class.getName());
	
	DataBase db = new DataBase();
	
	 /**
    * 声明数据库连接实例
    */
	//定义一个Connection 用来连接数据库 
	Connection conn=null;  
   //声明PreparedStatement实例
   PreparedStatement pstmt = null;
   //用于存放返回结果的数据集
   ResultSet rs = null;
	
	@SuppressWarnings("static-access")
	public int updateUserInfo(UserInfo ui,InsInfo ii,int user_type,int user_id) {
		StringBuffer sql = new StringBuffer();
		int j =0;
		if(user_type == 0){
			sql.append("update user_info set rownum=1");
			if(ui.getUsername() != null && !"".equals(ui.getUsername())){
				sql.append(", username = '"+ui.getUsername()+"'");
			}
			if(ui.getUserpic_path() != null && !"".equals(ui.getUserpic_path())){
				sql.append(", userpic_path = '"+ui.getUserpic_path()+"'");
			}
			if(ui.getMsisdn() != null && !"".equals(ui.getMsisdn())){
				sql.append(", msisdn = '"+ui.getMsisdn()+"'");
			}
			if(ui.getPasswd() != null && !"".equals(ui.getPasswd())){
				sql.append(", passwd = '"+ui.getPasswd()+"'");
			}
			if(ui.getSex() != null){
				sql.append(", sex = "+ui.getSex());
			}
			if(ui.getAddress() != null && !"".equals(ui.getAddress())){
				sql.append(", address = '"+ui.getAddress()+"'");
			}
			sql.append(" where userid ="+user_id);
		}else{
			sql.append("update ins_info set rownum=1");
			if(ii.getIns_name() != null && !"".equals(ii.getIns_name())){
				sql.append(", ins_name ='"+ii.getIns_name()+"'");
			}
			if(ii.getIns_sex() != null){
				sql.append(", ins_sex ="+ii.getIns_sex());
			}
			if(ii.getIns_addr() != null && !"".equals(ii.getIns_addr())){
				sql.append(", ins_addr ='"+ii.getIns_addr()+"'");
			}
			if(ii.getIns_age() != null){
				sql.append(", ins_age ="+ii.getIns_age());
			}
			if(ii.getIns_native_place() != null && !"".equals(ii.getIns_native_place())){
				sql.append(", ins_native_place ='"+ii.getIns_native_place()+"'");
			}
			if(ii.getIns_high() != null){
				sql.append(", ins_high = "+ii.getIns_high());
			}
			if(ii.getIns_weigh() != null){
				sql.append(", ins_weigh = "+ii.getIns_weigh());
			}
			if(ii.getSt_id() != null){
				sql.append(", st_id ="+ii.getSt_id());
			}
			if(ii.getHistory() != null && !"".equals(ii.getHistory())){
				sql.append(", history = '"+ii.getHistory()+"'");
			}
			if(ii.getTeach_year() != null){
				sql.append(", teach_year="+ii.getTeach_year());
			}
			if(ii.getTeach_way() != null && !"".equals(ii.getTeach_way())){
				sql.append(", teach_way='"+ii.getTeach_way()+"'");
			}
			if(ii.getTeach_pay() != null && !"".equals(ii.getTeach_pay())){
				sql.append(", teach_pay='"+ii.getTeach_pay()+"'");
			}
			if(ii.getIns_msisdn()!=null && !"".equals(ii.getIns_msisdn())){
				sql.append(", ins_msisdn='"+ii.getIns_msisdn()+"'");
			}
			if(ii.getInspic_path() != null && !"".equals(ii.getInspic_path())){
				sql.append(", inspic_path='"+ii.getInspic_path()+"'");
			}
			if(ii.getIns_passwd() != null && !"".equals(ii.getIns_passwd())){
				sql.append(", ins_passwd='"+ii.getIns_passwd()+"'");
			}
			if(ii.getIns_description() != null && !"".equals(ii.getIns_description())){
				sql.append(", ins_description='"+ii.getIns_description()+"'");
			}
			sql.append(" where ins_id ="+user_id);
		}
		System.out.println("updateUserInfo sql is: "+sql.toString());
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql.toString());
			j = pstmt.executeUpdate();
		} catch (SQLException e) {
			j = 0;
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
		return j;
	}
	
	@SuppressWarnings("static-access")
	public int updateInsFilePath(String id_path,String qc_path,String ins_honor,int ins_id){
		int i =0;
		StringBuffer sql = new StringBuffer();
		sql.append("update ins_info set id_path=?,qc_path=?,ins_honor=?,audit_results=? where ins_id=?");
		if(id_path == null){
			id_path = "";
		}
		if(qc_path == null){
			qc_path = "";
		}
		if(ins_honor == null){
			ins_honor = "";
		}
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql.toString());
			pstmt.setString(1,id_path);
			pstmt.setString(2, qc_path);
			pstmt.setString(3,ins_honor);
			pstmt.setInt(4,2);
			pstmt.setInt(5,ins_id);
			i = pstmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
		return i;
	}
	
	@SuppressWarnings("static-access")
	public int updateInsLocation(int ins_id,String ins_longitud,String ins_latitude){
		String sql = "update ins_location set ins_longitud=?,ins_latitude=?,update_time=? where ins_id=?";
		int i = 0;
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,ins_longitud);
			pstmt.setString(2,ins_latitude);
			pstmt.setTimestamp(3,new java.sql.Timestamp(System.currentTimeMillis()));
			pstmt.setInt(4,ins_id);
			i = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
		
		return i;
	}
	
	@SuppressWarnings("static-access")
	public int checkPasswd(String user_id,String user_type,String old_passwd){
		int i = 0;
		String sql = "";
		if("0".equals(user_type)){
			sql = "select count(*) from user_info where userid = ? and passwd = ?";
		}else{
			sql = "select count(*) from ins_info where ins_id = ? and ins_passwd = ?";
		}
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1,Integer.parseInt(user_id));
			logger.info("checkPasswd user_id value is :"+Integer.parseInt(user_id));
			pstmt.setString(2,Utils.encode(old_passwd.getBytes()));
			logger.info("checkPasswd old_passwd encode value is :"+Utils.encode(old_passwd.getBytes()));
			rs = pstmt.executeQuery();
			rs.next();
			i = rs.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	@SuppressWarnings("static-access")
	public int updatePasswd(String user_id,String user_type,String new_passwd){
		int i = 0;
		String sql = "";
		if("0".equals(user_type)){
			sql = "update user_info set passwd = ? where userid = ?";
		}else{
			sql = "update ins_info set ins_passwd = ? where ins_id = ?";
		}
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,Utils.encode(new_passwd.getBytes()));
			pstmt.setInt(2,Integer.parseInt(user_id));
			i = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	@SuppressWarnings("static-access")
	public int updateBinddingMsisdn(String new_msisdn,String user_type,String user_id){
		int i = 0;
		String sql = "";
		if("0".equals(user_type)){
			sql = "update user_info set msisdn = ? where userid = ?";
		}else{
			sql = "update ins_info set ins_msisdn = ? where ins_id = ?";
		}
		conn = db.getconn();
		try {
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,new_msisdn);
			pstmt.setInt(2,Integer.parseInt(user_id));
			i = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}

}
